package com.acerola.mrpc.handler;

import com.acerola.mrpc.protocol.RequestHolder;
import com.acerola.mrpc.protocol.RpcFuture;
import com.acerola.mrpc.message.RpcProtocol;
import com.acerola.mrpc.message.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @program: mrpc
 * @description: 客户端处理器
 * @author: Acerola
 * @create: 2022-12-12 19:21
 **/
@Slf4j
public class RpcClientHandler extends SimpleChannelInboundHandler<RpcProtocol<RpcResponse>> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcProtocol<RpcResponse> msg) throws Exception {
        log.debug("接收rpc返回结果");
        long requestId = msg.getHeader().getRequestId();
        //从map中取出接收的异步线程,并清除map,以免内存泄漏
        RpcFuture<RpcResponse> future = RequestHolder.REQUEST_MAP.remove(requestId);
        future.getPromise().setSuccess(msg.getContent());
    }
}
